<template>
  <header id="header">
    <section class="container">
      <h1 id="logo">
        <a href="#" title="霖霖学院">
          <img src="~/assets/img/小葵花课堂2.png" width="100%" alt="霖霖学院" />
        </a>
      </h1>
      <div class="h-r-nsl">
        <ul class="nav">
          <li>
            <NuxtLink to="/" exact active-class="page"> 首页 </NuxtLink>
          </li>
          <li>
            <NuxtLink to="/course" exact active-class="current"> 课程 </NuxtLink>
          </li>
          <li>
            <NuxtLink to="/teacher" exact active-class="current"> 名师 </NuxtLink>
          </li>
          <li>
            <NuxtLink to="/live" active-class="current"> 直播课 </NuxtLink>
          </li>
          <li>
            <NuxtLink :to="userStore.getUserId?'/user/' + userStore.getUserId:'/login'" active-class="current"> 个人中心 </NuxtLink>
          </li>
          <li>
            <NuxtLink to="/chat" active-class="current">智能问答</NuxtLink>
          </li>
        </ul>
        <!-- / nav -->
        <ul class="h-r-login">
          <li v-if="!isLogin" id="no-login">
            <a href="/login" title="登录">
              <em class="icon18 login-icon">&nbsp;</em>
              <span class="vam ml5">登录</span>
            </a>
            |
            <a href="/register" title="注册">
              <span class="vam ml5">注册</span>
            </a>
          </li>
          <li v-if="isLogin" id="is-login-two" class="h-r-user">
            <a :href="'/user/' + userStore.getUserId">
              <img :src="userStore.getAvatar" width="30" height="30" :alt="nickname" class="vam picImg inline-block"
                :title="nickname" />
            </a>
            <a href="javascript:void(0)" class="ml5" title="退出" @click="exit()">退出</a>
          </li>
          <!-- /未登录显示第1 li；登录后显示第2，3 li -->
        </ul>
        <aside class="h-r-search">
          <form action="#" method="post">
            <label class="h-r-s-box">
              <input type="text" placeholder="输入你想学的课程" name="queryCourse.courseName" value />
              <button type="submit" class="s-btn">
                <em class="icon18">&nbsp;</em>
              </button>
            </label>
          </form>
        </aside>
      </div>
    </section>
  </header>
</template>
<script setup lang="ts">
const isLogin = ref<boolean>(false);
const userStore = useUserStore()
const router = useRouter()
const exit = () => {
  const token = useCookie('lin-token')
  token.value = ''
  const userInfo = useCookie('userInfo')
  userInfo.value = ''
  userStore.userInfo = {
    userId: '',
    nickName: '',
    avatar: '',
    token: '',
  }
  router.push('/login')
};
const token = useCookie('lin-token')
userStore.userInfo.token = token.value || ''

// userStore.getUserInfo()
const userInfo = useCookie('userInfo')
if (userInfo.value) {
  userStore.userInfo.userId = (userInfo.value as any).userId
  userStore.userInfo.avatar = (userInfo.value as any).avatar
  userStore.userInfo.nickName = (userInfo.value as any).nickName
}
isLogin.value = userStore.getToken !== ''

const nickname = userStore.userInfo.nickName;

</script>

<style scoped>
.current {
  background-color: darkgreen;
  color: white;
  border-radius: 0.5rem;
}

.page {
  background-color: darkgreen;
  color: white;
  border-radius: 0.5rem;
}

.container {
  width: 1272px;
}

#header {
  height: 80px;
  padding: 0;
}
</style>